package com.platform.api;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.platform.entity.AddressEntity;
import com.platform.entity.UserEntity;
import com.platform.service.AddressService;
import com.platform.util.ApiBaseAction;
import com.platform.utils.R;

import io.swagger.annotations.Api;

@Api(tags = "API地址维护接口")
@RestController
@RequestMapping("/api/address")
public class ApiAddressController extends ApiBaseAction{
    private Logger logger = Logger.getLogger(getClass());

    @Autowired
    private AddressService addressService;
    
    @RequestMapping(value = "/save")
    public R save(@RequestBody AddressEntity address) {
        try {
        	UserEntity user = getCurrentUser();
        	address.setUserId(user.getId());
        	addressService.save(address);
            return R.ok().put("address", address);
        } catch (Exception e) {
        	return R.error(e.getMessage());
        }
    }
    
    @RequestMapping(value = "/saveaddtoorder/{orderId}")
    public R saveAddToOrder(@PathVariable("orderId") Integer orderId,@RequestBody AddressEntity address) {
        try {
        	UserEntity user = getCurrentUser();
        	address.setUserId(user.getId());
        	addressService.saveAddToOrder(address,orderId);
            return R.ok().put("address", address);
        } catch (Exception e) {
        	return R.error(e.getMessage());
        }
    }
    
    @RequestMapping(value = "/list")
    public R userList() {
        try {
        	UserEntity user = getCurrentUser();
        	Map<String,Object> params = new HashMap<String,Object>();
        	params.put("userId", user.getId());
        	List<AddressEntity> addresses = addressService.queryList(params);
        	
        	return R.ok().put("addresses", addresses);
        } catch (Exception e) {
        	return R.error(e.getMessage());
        }
    }
    
    @RequestMapping("/info/{addressId}")
    public R info(@PathVariable("addressId") Integer addressId) {
        try {
        	UserEntity user = getCurrentUser();
        	AddressEntity address = addressService.queryObject(addressId);
        	if(!address.getUserId().equals(user.getId())) {
            	return R.error("只能看自己的地址！");
        	}
        	return R.ok().put("address", address);
        } catch (Exception e) {
        	logger.error("ApiAddressController setAddressDefault:", e);
        	return R.error();
        }
    }
    
    @RequestMapping("/default/{addressId}")
    public R setAddressDefault(@PathVariable("addressId") Integer addressId,
    		ModelMap map) {
        try {
        	UserEntity user = getCurrentUser();
        	addressService.setDefault(addressId, user.getId());
        } catch (Exception e) {
        	logger.error("ApiAddressController setAddressDefault:", e);
        	return R.error();
        }
        return R.ok();
    }
    
}
